package com.weiaibenpao.demo.chislim.sport.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.sport.bean.RegionDataBean;
import com.weiaibenpao.demo.chislim.util.DateUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/24.
 */

public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.ViewHolder> {

    private OnItemClickListener listener;
    private Context mContext;
    private List<RegionDataBean> list;
    private DecimalFormat decimalFormat=new DecimalFormat("0.00");
    public  interface OnItemClickListener{
        void onItemClickListener(RegionDataBean bean);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener=listener;
    }
    public RecordAdapter(Context mContext) {
        this.mContext = mContext;
        list = new ArrayList<>();
    }

    public void refresh(List<RegionDataBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.record_item_layout, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final RegionDataBean bean = list.get(position);
        holder.itemCity.setText(bean.getProvince()+"·"+bean.getCity()+"·"+bean.getArea());
        holder.itemDistance.setText(decimalFormat.format(bean.getYudingDistance())+"KM");
        holder.itemRunDistance.setText(decimalFormat.format(bean.getTotalDistance())+"KM");
        holder.itemRunTime.setText(DateUtil.getFormatTime(bean.getTotalTime()));
        holder.itemRunLastTime.setText(DateUtil.getRecordDate(bean.getCreateTime()));
        if (bean.getYudingDistance()<=bean.getTotalDistance()){
            holder.isRun.setVisibility(View.VISIBLE);
        }else {
            holder.isRun.setVisibility(View.GONE);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClickListener(bean);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list!=null?list.size():0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_city)
        TextView itemCity;
        @BindView(R.id.item_distance)
        TextView itemDistance;
        @BindView(R.id.item_run_distance)
        TextView itemRunDistance;
        @BindView(R.id.item_run_time)
        TextView itemRunTime;
        @BindView(R.id.item_run_last_time)
        TextView itemRunLastTime;
        @BindView(R.id.item_img_run)
        ImageView isRun;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
